define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/damage/index',
                    add_url: 'box/damage/add',
                    edit_url: 'box/damage/edit',
                    del_url: 'box/damage/del',
                    multi_url: 'box/damage/multi',
                    table: 'damage',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'damage_id',
                sortName: 'damage_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'box_no', title: __('Box_no')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'area', title: __('Area'), formatter: Controller.api.formatter.city_info},
                        {field: 'room', title: __('Room'), formatter: Controller.api.formatter.house_info},
                        {field: 'contacts', title: __('Contacts')},
                        {field: 'telephone', title: __('Telephone')},
                        {field: 'damage_amount', title: __('Damage_amount'), operate: 'BETWEEN'},
                        {field: 'sum_amount', title: __('Sum_amount'), operate: 'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'damage_level', title: __('Damage_level')},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        }
                    ]
                ]
            });


            $(table).on("click", "[data-id].btn-hsxq", function (e) {
                var id = $(this).data("id");

                Backend.api.open('box/damage/detail/ids/' + id, __('货损详情'), {area: ["90%", "90%"]});

            });
            $(document).on("click", ".btn-adddamage", function () {
                Layer.prompt({title: __('请填写盒子编号'), formType: 0}, function (value, index) {
                    if (value == '') {
                        Layer.alert('请填写盒子编号');
                        return;
                    } else {
                        $.post('box/damage/getBoxByBoxNo', {box_no: value}, function (res) {
                            if (res.code == 1) {
                                Backend.api.open('box/damage/add/ids/' + res.data.user_box_id, __('添加货损记录'), {area: ["90%", "90%"]}, {
                                    callback: function (value) {
                                        table.bootstrapTable('refresh', {});
                                    },
                                });
                            } else {
                                Layer.alert(res.msg);
                                return;
                            }

                        });
                    }
                    Layer.close(index);
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                city_info: function (value, row, index) {
                    var html = row.province_name + '-' + row.city_name + '-' + row.area_name;
                    return html;
                },
                house_info: function (value, row, index) {

                    var html = row.quarters_name + '-' + row.unit_name + '-' + row.room_name;
                    return html;
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '';

                    html += '<a href="javascript:;" class="text-success btn-hsxq" style="cursor:pointer"  data-id="' + row.damage_id + '"><b>查看详情</b></a>';
                    return html;
                }
            },
        }
    };
    return Controller;
});